package main

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
)

// 定义一个全局的pool
var pool *redis.Pool

// 当启动程序时就初始化连接池
func init() {
	pool = &redis.Pool {
		MaxIdle: 8,
		MaxActive: 0,
		IdleTimeout: 100,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", "192.168.57.133:6379")
		},
	}
}

func main() {

	// 先从pool取出一个连接
	conn := pool.Get()
	defer conn.Close()

	_, err := conn.Do("Set", "name", "丁佑强")
	if err != nil {
		fmt.Println("do error: ", err)
	}

	name, err2 := redis.String(conn.Do("Get", "name"))
	if err2 != nil {
		fmt.Println("do error: ", err2)
	}

	fmt.Println("name =", name)

	conn.Close()
	pool.Close() // 再取会报错：get on closed pool

}